﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Configuration;
using System.Data.Common;
using System.Data;
using System.Data.OracleClient;

namespace Bank.Account
{
    public class DataVisitor
    {
        public static ConnectionStringSettings ConnectionSet = ConfigurationManager.ConnectionStrings["SIIM"];
        public static DbConnection Connection
        {
            get
            {
                var conn = DataFactory.CreateConnection();
                conn.ConnectionString = ConnectionSet.ConnectionString;
                conn.Open();
                return conn;
            }
        }
        static DataVisitor()
        {
            DataFactory = DbProviderFactories.GetFactory(ConnectionSet.ProviderName);
        }

        public static DbProviderFactory DataFactory { get; set; }

        public static void calltrade(decimal tradeno, string inparam, out string t)
        {
            var commd = Connection.CreateCommand();
            commd.CommandText = "pkg_Account.p_trade";
            commd.CommandType = CommandType.StoredProcedure;
            commd.Parameters.Add(COracleParameter("Prm_Tradetype", tradeno, OracleType.Number, ParameterDirection.Input));
            commd.Parameters.Add(COracleParameter("Prm_Indata", inparam, OracleType.VarChar, ParameterDirection.Input));
            commd.Parameters.Add(COracleParameter("Prm_Appcode", null, OracleType.Number, ParameterDirection.Output));
            commd.Parameters.Add(COracleParameter("Prm_Outdata", null, OracleType.VarChar, ParameterDirection.Output));
            commd.ExecuteNonQuery();
            t = commd.Parameters[2].Value.ToString() + "|" +
            commd.Parameters[3].Value.ToString();
            commd.Connection.Close();
        }

        public static void calltrade(decimal tradeno, string inparam, out string code, out string t)
        {
            var commd = Connection.CreateCommand();
            commd.CommandText = "pkg_Account.p_trade";
            commd.CommandType = CommandType.StoredProcedure;
            commd.Parameters.Add(COracleParameter("Prm_Tradetype", tradeno, OracleType.Number, ParameterDirection.Input));
            commd.Parameters.Add(COracleParameter("Prm_Indata", inparam, OracleType.VarChar, ParameterDirection.Input));
            commd.Parameters.Add(COracleParameter("Prm_Appcode", null, OracleType.Number, ParameterDirection.Output));
            commd.Parameters.Add(COracleParameter("Prm_Outdata", null, OracleType.VarChar, ParameterDirection.Output));
            commd.ExecuteNonQuery();
            code = commd.Parameters[2].Value.ToString();
            t = commd.Parameters[3].Value.ToString();
            commd.Connection.Close();
        }

        public static OracleParameter COracleParameter(string name, object value, OracleType ot, ParameterDirection pd)
        {
            var param = new OracleParameter();
            param.ParameterName = "" + name;
            if (pd != ParameterDirection.Output)
            {
                param.Value = value;
            }
            if (ot == OracleType.VarChar)
            {
                param.Size = 4000;
            }
            param.OracleType = ot;
            param.Direction = pd;
            return param;
        }
    }
}